/**
 * 
 */
package org.t2.game.server.foundation.starting;

import org.gaming.design.export.SlimJsonReader;
import org.gaming.design.loader.DesignCacheManager;
import org.gaming.design.loader.DesignDefine;
import org.gaming.ruler.eventbus.EventBus;
import org.gaming.ruler.lifecycle.Lifecycle;
import org.gaming.ruler.lifecycle.LifecycleInfo;
import org.gaming.ruler.lifecycle.Ordinal;
import org.gaming.ruler.lifecycle.Priority;
import org.gaming.service.timing.TimeSetting;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.t2.design.T2DesignParser;

/**
 * @author YY
 *
 */
@Component
public class ConfigLifecycle implements Lifecycle {

	private static Logger logger = LoggerFactory.getLogger(ConfigLifecycle.class);
	
	@Autowired
	private ApplicationSettings applicationSettings;
	@Autowired
	private TimeSetting timeSetting;
	
	public static DesignDefine designDefine;
	
	public LifecycleInfo getInfo() {
		return LifecycleInfo.valueOf(this.getClass().getSimpleName(), Priority.SYSTEM, Ordinal.MIN);
	}

	public void start() throws Exception {
		designDefine = new DesignDefine();
		designDefine.setNameOffset(3);
		designDefine.setTypeOffset(2);
		designDefine.setTagOffset(4);
		designDefine.setCommentOffset(1);
		designDefine.setBothTag("all");
		designDefine.setServerTag("server");
		designDefine.setClientTag("client");
		designDefine.setIgnoreTag("-");
		
		//0点刷新
		timeSetting.setOclock(0);
		EventBus.useTracer(applicationSettings.isGameDebugMode());
		logger.info("配置读取路径：{}", applicationSettings.getGameDesignFilePath());
		DesignCacheManager.loadFull(applicationSettings.getGameDesignFilePath(), new SlimJsonReader(), new T2DesignParser(), true, designDefine);
	}
	
	public void reloadConfig() {
		DesignCacheManager.loadFull(applicationSettings.getGameDesignFilePath(), new SlimJsonReader(), new T2DesignParser(), true, designDefine);
	}
}
